<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_INS_SHOP_SHOP.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_INS_SHOP_SHOP,$login['staff_auth']);

$dosearch = FALSE ;
$dao = new Class_M_INS_SHOP_SHOP;

$isG5  = FALSE;
if   ($login['staff_auth'] == 'G-5') {
	$isG5 = TRUE;
}

if ( ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download"  ||   $_GET['csv'] == "copy"  ||   $_GET['csv'] == "updins" ) ) ||
	 (isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) ){
	 $forms =  $_SESSION['MSEARCH']['INS_SHOP_SHOP'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['INS_SHOP_SHOP'] = $forms;
}

if ( isset( $forms['matrix_cd'] ) &&  ( !empty( $forms['matrix_cd'] ) ) ){
	$dao -> setTableNameWeek( $forms['matrix_cd'] );
 }

$wherearr =  array();
$param    =  NULL;
if ( isset( $forms['shop_cd'] ) &&  ( !empty( $forms['shop_cd'] ) ) ){
	$wherearr[] = sprintf(" b.shop_cd =  '%s' ",  $dao->esc($forms['shop_cd']) );
}

if ( isset( $forms['shop_name'] ) &&  ( !empty( $forms['shop_name'] ) ) ){
	$wherearr[] = sprintf(" b.shop_name like  '%s' ",  "%".$dao->esc($forms['shop_name']."%") );
}

if ( isset( $forms['ins_shop_name'] ) &&  ( !empty( $forms['ins_shop_name'] ) ) ){
	//$wherearr[] = sprintf(" c.ins_shop_name like  '%s' ",  "%".$dao->esc($forms['ins_shop_name']."%") );
	$param['ins_shop_name'] =  $forms['ins_shop_name'] ;
}

if ( isset( $forms['ins_shop_cd'] ) &&  ( !empty( $forms['ins_shop_cd'] ) ) ){
	//$wherearr[] = sprintf(" c.ins_shop_cd like  '%s' ",  "%".$dao->esc( $forms['ins_shop_cd'] )."%" );
	$param['ins_shop_cd'] =  $forms['ins_shop_cd'] ;
}

//地域広告紐付
if ( isset( $forms['area_disp_ng']  )  &&  ( strlen( $forms['area_disp_ng'] ) > 0 ) ){
	$wherearr[] = sprintf(" a.area_disp_ng =  '%s' ",  $forms['area_disp_ng'] );
	//$param['area_disp_ng'] =  $forms['area_disp_ng'] ;
}

//非表示設定
if ( isset( $forms['disp_ng']  )  &&  ( strlen( $forms['disp_ng'] ) > 0 ) ){
	$wherearr[] = sprintf(" a.disp_ng =  '%s' ",  $forms['disp_ng'] );
}

//効なGEOコードの重複チェック 
if ( isset( $forms['repeat_flg']  )  &&  ( strlen( $forms['repeat_flg'] ) > 0 ) ){
	$repeat =  1 ;
}
if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = str_replace( "u_time", "a.u_time", Class_PWD::get_u_time_where_sql($forms['upd_f'], 1 ) );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = str_replace( "u_time", "a.u_time", Class_PWD::get_u_time_where_sql($forms['upd_t'],2 ) );
}

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "ins_shop_shop".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$page   = NULL ;
	$data   = $dao->search( $wherearr, $page, $param, $repeat) ;
	$colums = array() ;
	$col_heads = array(
		'matrix_cd'       => array( 'マトリックスコード', 1),
		'year4'           => array( '年度',             1),
		'week_cd'         => array( '週',               1),
		'flier_date'      => array( '折込日',           1),
		'shop_cd'         => array( '店舗コード',  1),
		'shop_name'       => array( '店舗名',  1),
		'ins_shop_cd'     => array( 'GEOコード',  1),		
		'ins_shop_name'   => array( '販売店名',  1),
		'shop_cd'         => array( '折込会社部数',  1),
		'area_disp_ng '   => array( '地域広告紐付',  1),
		'area_pattern_nm' => array( '地域新聞名',  1),
		'area_adnums'     => array( '地域広告部数',  1),
		'area_memo'       => array( '地域広告備考',  1),
		'memo'            => array( '備考',  1),
		'disp_ng'         => array( '非表示フラグ',  1),			
		'u_time'          => array( '更新日時',        3),
		'c_time'          => array( '登録日時',        3),
		'm_user_name'     => array( '更新者',          1)
	);


	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	$week = Class_M_OTHER::get_by_matrix_cd($forms['matrix_cd'] );
	
	$week["flier_date"] =  Class_PWD::int8_to_dateD($week["flier_date"]);
	
	$pattern  = Class_M_OTHER::get_pattern_array(NULL, '23' );
	//データ整形開始
	foreach ($data as $k => $v) {
		
		$data[$k]["area_disp_ng "]  = $v['area_disp_ng '] == '1'? '無効' :'有効';
		$data[$k]["c_time"]      =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
		$data[$k]["area_pattern_nm"]   = $pattern[ $v["area_pattern_cd"]];
		$data[$k]["matrix_cd"]   = $forms['matrix_cd'];
		$data[$k]["year4"]       =  $week["year4"];
		$data[$k]["week_cd"]     =  $week["week_cd"];
		$data[$k]["flier_date"]  =  $week["flier_date"];
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}

//---CSVダウンロード　処理完了

if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];
}else{
	$page['current'] = 1;
}

$page['item'] = PAGE_MAX;

if ($dosearch) {
	//$page内容は関数内部更新されます。
	//---コピー　処理開始
	if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "copy" ) ){
			$copy_matrix_cd =   $_GET['tweek'] ;
			$data = $dao->search( $wherearr, $page, $param,$repeat,$copy_matrix_cd );
			if ($copy_matrix_cd == "true" ){
				$copymsg = "コピー成功しました。";
			}else {
				$copymsg = "コピー失敗しました。";
			}
	}else if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "updins" ) ){
		$copy_cd = null;
		$upd_ins = true;
		$data = $dao->search( $wherearr, $page, $param, $repeat, $copy_cd, $upd_ins);
		if ($upd_ins == "true" ){
				$updinsmsg = "結果の販売店情報を更新する成功しました。";
			}else {
				$updinsmsg = "結果の販売店情報を更新する失敗しました。";
			}
	} else {
		$data = $dao->search( $wherearr, $page, $param,$repeat );
	}

}


//表示ために整形開始
foreach ($data as $k => $v ){
	$disp_aid = $v["area_disp_ng"] == '1' ? 1 : 0;
	$data[$k]["area_disp"] = $aoki_area_user_flags[$disp_aid];
}

$disp = array();
$old_shop_id = "";
$rows = 0;

foreach ($data as $k => $v ) {
	if ($old_shop_id != $v["shop_cd"]) {
		if (!empty($old_shop_id)) {
			$disp[] = $rows;
		}
		$data[$k]["rowspan"] = 1 ;
		$rows = 1;
		$old_shop_id = $v["shop_cd"];
	} else {
		$rows = $rows + 1;
		$data[$k]["rowspan"] = 0 ;
	}
}
$disp[] = $rows;
$index = 0;
foreach ($data as $k => $v ) {
	if ($data[$k]["rowspan"] == 1 ) {
		
		$data[$k]["rowspannums"] = $disp[$index];
		if  (($index + 1) % 2 ==0) {
			$data[$k]["rowspanclass"] = "mod" ;
		}else {
			$data[$k]["rowspanclass"] = "odd" ;
		}
		
		
		$index = $index + 1;
	}
}

if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}


if (empty($forms["year4c"])) {
	$forms["year4c"] = date('Y');
}


//表示ために整形完了
if( isset($forms) ) $smarty->assign('forms', $forms);

if( isset($data) )  $smarty->assign('data', $data);
//if( isset($disp) )  $smarty->assign('disp', $disp);
if( isset($page) )  $smarty->assign('page', $page);

if( isset($updinsmsg) )  $smarty->assign('updinsmsg', $updinsmsg );
if( isset($copymsg) )   $smarty->assign('copymsg', $copymsg );

/*

$block = Class_M_OTHER::get_block_array( PLEASE_SELECT_ARR );
$smarty->assign('block', $block );

$local= Class_M_OTHER::get_local_array( PLEASE_SELECT_ARR );
$smarty->assign('local', $local );
*/

$flags[""] = "すべて";
foreach ($aoki_disp_flags as $k => $v) {
	$flags[$k] = $v;
}
$areaflags[""] = "すべて";
foreach ($aoki_area_user_flags as $k => $v) {
	$areaflags[$k] = $v;
}
$smarty->assign('isG5', $isG5 );
$smarty->assign('year4',  Class_PWD::get_years());

$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR );
$smarty->assign('week', $week );

$weekc= Class_M_OTHER::get_week_array( $forms["year4c"], PLEASE_SELECT_ARR );
$smarty->assign('weekc', $weekc );

$smarty->assign('flags', $flags );
$smarty->assign('areaflags', $areaflags );

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('minsshopshop_index.html');
?>